//
//  YSStatusViewModel.swift
//  新浪浪
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YSStatusViewModel: NSObject {

    var statusModel : YSStatusModel?
    var picture: YSPictureModel?
    override init() {
        super.init()
    }
    
    func dealSource(Str:String?)->String?{
        guard let source = Str else {
            return nil
        }
        let sourceArray = source.componentsSeparatedByString(">")
        if sourceArray.count >= 2{
            let subSource = sourceArray[1]
            let lastSourceArray = subSource.componentsSeparatedByString("<")
            if  lastSourceArray.count >= 1 {
                return lastSourceArray.first
            }else {
                return nil
            }
        }else {
            return nil
        }
    }
    
    
    func dealCreateDate(create_date:String?) ->String?{
        guard let createDate = create_date else {
            return nil
        }
        
        let formatter = NSDateFormatter()
        
        formatter.locale = NSLocale(localeIdentifier: "cn")
        
        //        "Mon Mar 07 12:46:42 +0800 2016"
        
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        let date = formatter.dateFromString(createDate)
        
        let calendar = NSCalendar.currentCalendar()
        
        let unites = NSCalendarUnit (arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
        
        if let Odate = date {
            let compts = calendar.components(unites, fromDate: Odate, toDate: NSDate(), options: [])
            
            if compts.year == 0{//今年
                if compts.month == 0{//这个月
                    if compts.day == 0{//今天
                        if compts.hour == 0 {//xx分钟前
                            if compts.minute == 0{
                                return "刚刚"
                            }else {
                                return "\(compts.minute)分钟前"
                            }
                        }else {//N个小时以前
                            return "\(compts.hour)小时前"
                        }
                    }else if compts.day == 1{
                        formatter.dateFormat = "HH:mm"
                        let str = formatter.stringFromDate(Odate)
                        return "昨天\(str)"
                    }
                    else {//N天前
                        //                        formatter.dateFormat =
                        return "\(compts.day)天前"
                    }
                }else {//几个月以前
                    return "\(compts.month)月前"
                }
            }else {//一年前
                return "\(compts.year)年前"
            }
            
            
            
        }
        
        
        
        
        return "\(date)"
    }
    
    
    
    var verifity_image:UIImage? {
        switch statusModel?.user?.verified_type ?? -1{
        case -1: return nil
        case 0 : return UIImage (named: "avatar_vip")
        case 2,3,5: return UIImage (named: "avatar_enterprise_vip")
        case 220: return UIImage (named: "avatar_grassroot")
        default:return nil
        }
    }
    
    var level_image:UIImage? {
//        print(statusModel?.user?.mbrank)
        return UIImage (named: "common_icon_membership_level\(statusModel?.user?.mbrank ?? 0 )")
        
    }
    
    
    
    var retweetContent:String?{
        return statusModel?.retweeted_status?.text
    }
    
    
    var retweetString: String? {
        if statusModel?.reposts_count == 0 {
            return "转发"
        }else {
            if let str = statusModel?.reposts_count {
                return "\(str)"
            }
                return "转发"
            
        }
    }
    //评论
//    var commentString : String?
//    
//    var retweetS
//    tatusString: String? {
//        return statusModel?.retweeted_status?.text
//    }
}





